package com.my.yyr;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
import org.springframework.context.annotation.Bean;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

import javax.servlet.*;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@SpringBootApplication
@EnableZuulProxy
public class YyrZuulApplication {

    public static void main(String[] args) {
        SpringApplication.run(YyrZuulApplication.class, args);
    }

/*
    @Bean
    public FilterRegistrationBean<CorsFilter> corsFilter() {

        final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();

        final CorsConfiguration config = new CorsConfiguration();
        // 允许cookies跨域,可以是进行session跟踪
        config.setAllowCredentials(true);

        // 允许向该服务器提交请求的URI，*表示全部允许，在SpringMVC中，如果设成*，会自动转成当前请求头中的Origin
        config.addAllowedOrigin("*");

        // 允许访问的头信息,*表示全部
        config.addAllowedHeader("*");

        // 预检请求（options请求）的缓存时间（秒），即在这个时间段里，对于相同的跨域请求不会再预检了
        config.setMaxAge(30L);

        // 允许提交请求的方法，*表示全部允许
        config.addAllowedMethod("*");
        */
/*
        config.addAllowedMethod("OPTIONS");
        config.addAllowedMethod("HEAD");
        config.addAllowedMethod("GET");
        config.addAllowedMethod("PUT");
        config.addAllowedMethod("POST");
        config.addAllowedMethod("DELETE");
        config.addAllowedMethod("PATCH");
        *//*


        //设置允许跨域的路径
        source.registerCorsConfiguration("/**", config);

        FilterRegistrationBean<CorsFilter> bean = new FilterRegistrationBean<CorsFilter>(new CorsFilter(source));

        // 设过滤器的优先级
        bean.setOrder(0);

        return bean;
    }
*/


    @Bean
    @Order(Ordered.HIGHEST_PRECEDENCE)//该过滤器最先处理
    public CorsFilter corsFilter() {

        final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();

        final CorsConfiguration config = new CorsConfiguration();
        // 允许cookies跨域,可以是进行session跟踪
        config.setAllowCredentials(true);

        // 允许向该服务器提交请求的URI，*表示全部允许，在SpringMVC中，如果设成*，会自动转成当前请求头中的Origin
        config.addAllowedOrigin("*");

        // 允许访问的头信息,*表示全部
        config.addAllowedHeader("*");

        // 预检请求（options请求）的缓存时间（秒），即在这个时间段里，对于相同的跨域请求不会再预检了
        config.setMaxAge(30L);

        // 允许提交请求的方法，*表示全部允许
        config.addAllowedMethod("*");

        /*
        config.addAllowedMethod("OPTIONS");
        config.addAllowedMethod("HEAD");
        config.addAllowedMethod("GET");
        config.addAllowedMethod("PUT");
        config.addAllowedMethod("POST");
        config.addAllowedMethod("DELETE");
        config.addAllowedMethod("PATCH");
        */


        //设置允许跨域的路径
        source.registerCorsConfiguration("/**", config);

        return new CorsFilter(source);


    }



    //注册禁用浏览器缓存过滤器
    @Bean
    public FilterRegistrationBean<Filter> noBrowerCacheFilter() {

        FilterRegistrationBean<Filter> bean = new FilterRegistrationBean<Filter>(new Filter() {

            @Override
            public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
                    throws IOException, ServletException, IOException {


                HttpServletResponse resp = (HttpServletResponse) response;
                resp.addHeader("Pragma", "no-cache");
                resp.setHeader("Cache-Control","no-cache,no-store,must-revalidate");
                resp.addHeader("Cache-Control","pre-check=0, post-check=0");
                resp.setDateHeader("Expires", 0);

                chain.doFilter(request, response);

            }

        });

        bean.addUrlPatterns("/*");
        bean.setOrder(1);

        return bean;

    }

}
